import { Label, RichText, tween } from "cc";

export default class LabelUtils{
    
    /**
     * 滚动数字效果
     * @param label 要滚动的文本节点
     * @param duration 花费时间
     * @param beginNum 起点值
     * @param endNum 终点值
     * @param proStr 后面拼接字符,没有就不拼
     * @param toFixed 小数点后保留几位
     */
     static updatenNumberAnim(label: Label | RichText, duration: number, beginNum: number, endNum: number, preStr: string = null, proStr: string = null, toFixed: number = 0): Promise<void> {
        return new Promise<void>((resolve, reject) => {
            let obj = {
                num: beginNum
            };
            label.string = `${obj.num}${proStr || ""}`;
            tween(obj).to(duration, { num: endNum }, {
                progress: (start: number, end: number, current: number, t) => {
                    label.string = `${preStr || ""}${current.toFixed(toFixed)}${proStr || ""}`
                    return start + (end - start) * t;
                }
            })
                .call(() => {
                    label.string = `${preStr || ""}${endNum}${proStr || ""}`
                    resolve();
                })
                .start();

        })
    }

    //格式化数字,不足10,加0
    static formatNumber(num: number): string {
        return num >= 10 ? `${num}` : `0${num}`
    }

    

    static setText(label: Label | RichText, text: string) {
        label.string = text
    }

}